package com.dhcc.bpm.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhcc.bpm.common.exception.BusinessException;
import com.dhcc.bpm.modules.system.entity.BusinessParams;
import com.dhcc.bpm.modules.system.mapper.BusinessParamsMapper;
import com.dhcc.bpm.modules.system.service.IBusinessParamsService;
import com.dhcc.bpm.modules.system.vo.BusinessParamsVO;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringPool;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.util.List;

import static org.springblade.core.cache.constant.CacheConstant.DICT_CACHE;

/**
 * @ClassName DictServiceImpl
 * @Description  服务实现类
 * @Author sjm
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@Service("blade_business")
@AllArgsConstructor
public class BusinessParamsServiceImpl extends ServiceImpl<BusinessParamsMapper, BusinessParams> implements IBusinessParamsService {

	BusinessParamsMapper dictMapper;
	/**
	 * 自定义分页
	 * @Author sjm
	 * @Date 19:10 2020/2/21 0021
	 * @param page
	 * @param dict
	 * @return com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.system.vo.DictVO>
	 **/
	@Override
	public IPage<BusinessParamsVO> selectDictPage(IPage<BusinessParamsVO> page, BusinessParamsVO dict) {
		return page.setRecords(baseMapper.selectDictPage(page, dict));
	}

	/**
	 * 树形结构
	 * @Author sjm
	 * @Date 19:10 2020/2/21 0021
	 * @param
	 * @return java.util.List<com.dhcc.bpm.modules.system.vo.DictVO>
	 **/
	@Override
	public List<BusinessParamsVO> tree() {
		return ForestNodeMerger.merge(baseMapper.tree());
	}

	/**
	 * 获取字典表对应中文
	 * @Author sjm
	 * @Date 19:10 2020/2/21 0021
	 * @param code
	 * @param dictKey
	 * @return java.lang.String
	 **/
	@Override
	public String getValue(String code, String dictKey) {
		return Func.toStr(baseMapper.getValue(code, dictKey), StringPool.EMPTY);
	}

	/**
	 * 获取字典表
	 * @Author sjm
	 * @Date 19:10 2020/2/21 0021
	 * @param code
	 * @return java.util.List<com.dhcc.bpm.modules.system.entity.Dict>
	 **/
	@Override
	public List<BusinessParams> getList(String code) {
		return baseMapper.getList(code);
	}

	/**
	 * 新增或修改
	 * @Author sjm
	 * @Date 19:10 2020/2/21 0021
	 * @param dict
	 * @return boolean
	 **/
	@Override
	@CacheEvict(cacheNames = {DICT_CACHE}, allEntries = true)
	public boolean submit(BusinessParams dict) {
		LambdaQueryWrapper<BusinessParams> lqw = Wrappers.<BusinessParams>query().lambda().eq(BusinessParams::getCode, dict.getCode()).eq(BusinessParams::getDictKey, dict.getDictKey());
		Integer cnt = Math.toIntExact(baseMapper.selectCount((Func.isEmpty(dict.getId())) ? lqw : lqw.notIn(BusinessParams::getId, dict.getId())));
		if (cnt > 0) {
			throw new BusinessException("当前字典键值已存在!");
		}
		if (Func.isEmpty(dict.getParentId())) {
			dict.setParentId(BladeConstant.TOP_PARENT_ID);
		}
		dict.setIsDeleted(BladeConstant.DB_NOT_DELETED);
		return saveOrUpdate(dict);
	}

	/**
	 * 删除字典
	 * @Author sjm
	 * @Date 19:10 2020/2/21 0021
	 * @param ids
	 * @return boolean
	 **/
	@Override
	public boolean removeDict(String ids) {
		Integer cnt = Math.toIntExact(baseMapper.selectCount(Wrappers.<BusinessParams>query().lambda().in(BusinessParams::getParentId, Func.toLongList(ids))));
		if (cnt > 0) {
			throw new BusinessException("请先删除子节点!");
		}
		return dictMapper.deleteById(Func.toLongList(ids));
	}
}
